package systempen.cmanager;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

public class PasswordSet extends Activity {

	EditText _tvPassword;
	EditText _tvPasswordQuestion;
	EditText _tvPasswordAnswer;
	Button _btnSave;
	Button _btnCancel;



	@Override
	public void onCreate(Bundle savedInstanceState){
		super.onCreate(savedInstanceState);
		//ThemePicker.onActivityCreateSetTheme(this);
		ThemePicker.onActivityCreateSetTheme(this);
		setContentView(R.layout.password_insert);
		_btnSave = (Button) findViewById(R.id.password_save);
		_btnCancel = (Button) findViewById(R.id.password_cancel);
		_tvPassword = (EditText) findViewById(R.id.password_text);
		_tvPasswordQuestion = (EditText) findViewById(R.id.password_questrion);
		_tvPasswordAnswer = (EditText) findViewById(R.id.password_answer);
	}

	public void onSave(View v){
		String pass = _tvPassword.getText().toString();
		String question = _tvPasswordQuestion.getText().toString();
		String answer = _tvPasswordAnswer.getText().toString();
		String sql;
		if (pass.equals(""))
		{
			util.execSQL("delete from password", getApplicationContext());
			Toast.makeText(this, "Password deleted and disabled.", Toast.LENGTH_SHORT).show();
		}else
		{
			if (util.PasswordExists(getApplicationContext())) //if exists update it
			{
						
				sql = " update password set pass = \'"+pass+"\', question = \'"+question+"\', answer = \'"+answer+"\' where rowid = 1 ";
				util.execSQL(sql, getApplicationContext());
			}
			else//new password
			{
				sql = "insert into password (pass, question, answer) values (\'"+pass+"\',\'"+question+"\',\'"+answer+"\')";
				util.execSQL(sql, getApplicationContext());
			}
		}
		Toast.makeText(this, "Password updated.", Toast.LENGTH_SHORT).show();
		startActivity(new Intent(this, MainActivityTab.class));
	}
	public void onCancel(View v){
		startActivity(new Intent(this, MainActivityTab.class));
	}


}
